#include "client_service.h"

Account CurAccount;

int ClientService::reserveBook(unsigned int book_id)
{
	BookServiceData bookdata;
	Book* book = bookdata.load(book_id);
	if(NULL == book || book->m_isShelf ==0 )
	{
		return -1; //未找到这本书
	}
	
	time_t timep;
    struct tm *p;
    time(&timep);
    p=localtime(&timep);
	for(vector<Account::Reserve>::iterator it = CurAccount.m_reserve.begin();it != CurAccount.m_reserve.end() ;it++)
    {
         time_t old = mktime(&it->m_time);
         time_t cur = timep;
         unsigned int num = 0;
         num = cur - old;
         if(book->m_id == it->m_bookId && num < 60)
         {
         	return -2;  //已预约这本书
//          CurAccount.m_reserve.erase(it);	
         }
    }
    
	int position = book->m_reserve.size() +1;
	Account::Reserve res ;
	Book::Reserve res1 ;

    res.m_time = *p;
    res1.m_time = *p;
	res.m_bookId = book->m_id;
	res1.m_bookId = book->m_id;
	res.m_id = CurAccount.m_id;
	res1.m_id = CurAccount.m_id;
	CurAccount.m_reserve.push_back(res);
	book->m_reserve.push_back(res1);
	
	position += book->m_amout*1000;
	BookServiceData m;
	m.save(*book);
	delete book;
	return position;
	
}
	
vector<BookInfo> ClientService::reserveBook(const char* book_name)
{
	return BookIndex.findBook(book_name);
}

Account* ClientService::queryUser(void)
{
	return &CurAccount;
}

Book* ClientService::queryBook(unsigned int book_id)
{
	BookServiceData bookdata;
	return bookdata.load(book_id);
}
		
vector<BookInfo> ClientService::queryBook(const char* book_name)
{
	return BookIndex.findBook(book_name);
}

BookList* ClientService::findBookList(unsigned int booklist_id)
{
	return BookListLoad(booklist_id);
}

bool ClientService::createBookList(unsigned int id,const char* list_name)
{
	BookList my_book_list(id,list_name);
	CurAccount.m_vecList.push_back(id);
	BookListSave(my_book_list);
	return true;
} 
